package com.whfc.emp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.AppEmpMapper;
import com.whfc.emp.dao.AppEmpRiskEmpMapper;
import com.whfc.emp.dao.AppEmpRiskMapper;
import com.whfc.emp.dto.AppEmpRiskDTO;
import com.whfc.emp.entity.AppEmp;
import com.whfc.emp.entity.AppEmpRiskEmp;
import com.whfc.emp.service.AppEmpRiskService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 风险告知书
 *
 * @author qzexing
 * @version 1.0
 * @date 2021/9/29 11:29
 */
@DubboService(interfaceClass = AppEmpRiskService.class, version = "1.0.0")
public class AppEmpRiskServiceImpl implements AppEmpRiskService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppEmpRiskMapper appEmpRiskMapper;

    @Autowired
    private AppEmpRiskEmpMapper appEmpRiskEmpMapper;

    @Autowired
    private AppEmpMapper appEmpMapper;

    @Override
    public PageData<AppEmpRiskDTO> list(Integer empId, Integer pageNum, Integer pageSize) throws BizException {
        logger.info("获取风险告知书列表, empId:{}, pageNum:{}, pageSize:{}", empId, pageNum, pageSize);
        AppEmp appEmp = appEmpMapper.selectByPrimaryKey(empId);
        if (appEmp == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该人员不存在.");
        }
        PageHelper.startPage(pageNum, pageSize);
        //获取风险告知书列表
        List<AppEmpRiskDTO> list = appEmpRiskMapper.selectRiskList(appEmp.getDeptId());
        PageHelper.clearPage();
        for (AppEmpRiskDTO appEmpRiskDTO : list) {
            //获取风险告知书签名情况
            AppEmpRiskEmp riskEmp = appEmpRiskEmpMapper.selectEmpList(appEmpRiskDTO.getRiskId(), empId);
            if (riskEmp != null) {
                appEmpRiskDTO.setSignTime(riskEmp.getSignTime());
                appEmpRiskDTO.setSignImgUrl(riskEmp.getSignImgUrl());
            }
        }
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void saveRiskSignImg(Integer riskId, Integer empId, String imgUrl) throws BizException {
        logger.info("保存风险告知书签名图片, riskId:{}, empId:{}, imgUrl:{}", riskId, empId, imgUrl);
        if (empId == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该人员不存在.");
        }
        AppEmpRiskEmp riskEmp = appEmpRiskEmpMapper.selectEmpList(riskId, empId);
        Date date = new Date();
        if (riskEmp != null) {
            riskEmp.setSignImgUrl(imgUrl);
            riskEmp.setSignTime(date);
            appEmpRiskEmpMapper.updateByPrimaryKeySelective(riskEmp);
        } else {
            riskEmp = new AppEmpRiskEmp();
            riskEmp.setRiskId(riskId);
            riskEmp.setEmpId(empId);
            riskEmp.setSignTime(date);
            riskEmp.setSignImgUrl(imgUrl);
            appEmpRiskEmpMapper.insertSelective(riskEmp);
        }
    }
}
